#!/bin/bash

# detailed user agent(UA) format: $remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"

# counting IP

# split each piece of record using character ' ', then $1 is $remote_addr

cat access.csv | awk -F ' ' '{ print $1 }' | sort | uniq -c | awk -F ' ' '
    BEGIN { 
        print "Counting IP";
        print "The IPs that visited the nginx server:"
        maxOccur = 0; 
        maxAddr = ""; 
    } 
    { 
        if ( $1 > maxOccur) { 
            maxOccur = $1; 
            maxAddr = $2; 
        } 
        print $2; 
    } 
    END { 
        printf "The IP that visited the most times is %s, %s times in total.\n\n", maxAddr, maxOccur 
    }'

# counting explorers

# split each piece of record using character '"', then $6 is $http_user_agent

# More information: https://en.wikipedia.org/wiki/User_agent
# User agent tool: https://useragent.buyaocha.com
# It should be declared that Edge has a UA ends with `Edg/version`, instead of `Edge/version`, this conclusion can be obtained by `access.csv` and test using Edge.
# Here, the method of classifying the explorers using the special matching, like unique identifier, although it may not always be accurate.

cat access.csv | awk -F '"' '{ print $6 }' | awk '{
    if ( $0 ~ /Edg/ ) {
        print "#Microsoft Edge"; # Here the character `#` denotes the separator between the count and the name. Otherwize, the processing of split may be difficult in next steps. 
    }
    else if ( $0 ~ /Firefox/ ) { 
        print "#Firefox";
    }
    else if ( $0 ~ /Chromium/ ) {
        print "#Chromium";
    }
    else if ( $0 ~ /Chrome/ || $0 ~ /CriOS/ ) {
        print "#Chrome";
    }
    else if ( $0 ~ /Safari/ ) {
        print "#Safari";
    }
    else if ( $0 ~ /Android/ && $0 ~ /WebKit/ ) {
        print "#WebKit";
    }
    else if ( $0 ~ /MSIE/ || $0 ~ /Trident/ ) {
        print "#MSIE";
    }
    else {
        printf "#%s\n", $0;
    }
}' | sort | uniq -c | awk -F '#' '
    BEGIN { 
        totCnt = 0;
        print "Counting explorers"
        print "Percent/%  Explorer Name";
    }
    {
        cnt[NR] = $1;
        totCnt += cnt[NR];
        name[NR] = $2;
    }
    END {
        for (i = 1; i <= NR; ++i) {
            printf "%8.3f%%  %-20s\n", 100 * cnt[i] / totCnt, name[i]
        }
        print "";
    }'

# counting systems information

cat access.csv | awk -F '"' '{ print $6 }' | awk '{ 
    st = index($0, "(");
    en = index($0, ")");
    # Get the information from the first pair of bracket
    if (st && en) {
        info = substr($0, st+1, en-st-1);
    }
    else{
        info = $0;
    }
    # print info;
    if ( info ~ /iPhone/ ) {
        print "#iOS"; # Here the character `#` denotes the separator between the count and the name. Otherwize, the processing of split may be difficult in next steps. 
    }
    else if ( info ~ /iPad/ ) {
        print "#iPadOS";
    }
    else if ( info ~ /Mac OS/ ) {
        print "#macOS";
    }
    else if ( info ~ /Windows NT/ ) {
        print "#Windows";
    }
    else if ( info ~ /Android/ ) {
        print "#Android";
    }
    else if ( info ~ /Linux/ ) {
        print "#Linux";
    }
    else if ( info ~ /OpenBSD/ ) {
        print "#openBSD";
    }
    else {
        printf "#%s\n", info;
    }
 }' | sort | uniq -c | awk -F '#' '
    BEGIN { 
        totCnt = 0;
        print "Counting systems"
        print "Percent/%  System Name";
    }
    {
        cnt[NR] = $1;
        totCnt += cnt[NR];
        name[NR] = $2;
    }
    END {
        for (i = 1; i <= NR; ++i) {
            printf "%8.3f%%  %-20s\n", 100 * cnt[i] / totCnt, name[i]
        }
        print "";
    }'
